package uni.UNIBEE7B8D.view.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTabHost;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;

import uni.UNIBEE7B8D.BaseActivity;
import uni.UNIBEE7B8D.R;
import uni.UNIBEE7B8D.view.Serve.ServerFragment;
import uni.UNIBEE7B8D.view.Study.StudyFragment;
import uni.UNIBEE7B8D.view.User.LoginActivity;
import uni.UNIBEE7B8D.view.User.userMessage.UserMessageActivity;
import uni.UNIBEE7B8D.view.Work.WorkFragment;

public class MainActivity extends BaseActivity {
    private FragmentTabHost tabHost;
    private View view1,view2,view3,view4;
    //private View viewHb;
    private String[] str = {"学习","工作台","服务","我"};
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences =getSharedPreferences("kangconfig", Activity.MODE_PRIVATE);
        tabHost = (FragmentTabHost) findViewById(R.id.tabHost);
        //tabHost.getTabWidget().setDividerDrawable(null); // 去掉分割线

        view1 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_view,null);
        view2 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_work,null);
//        view3 = LayoutInflater.from(getBaseContext()).inflate(R.layout.baoliao_lauout,null);
        view3 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_server,null);
        view4 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_user,null);
        //viewHb = LayoutInflater.from(getBaseContext()).inflate(R.layout.service_hb,null);
        try{
            tabHost.setup(this,getSupportFragmentManager(),R.id.fl);

            Fragment mainFragment = new StudyFragment();
            TabHost.TabSpec tabSpec0 =
                    tabHost.newTabSpec(str[0]).setIndicator(view1);
            tabHost.addTab(tabSpec0,mainFragment.getClass(),null);

            //            Fragment SecondFragment = new VideoFragment();
//            Fragment SecondFragment = new  MinshengWebFragment();
            Fragment SecondFragment = new WorkFragment();
            TabHost.TabSpec tabSpec1 =
                    tabHost.newTabSpec(str[1]).setIndicator(view2);
            tabHost.addTab(tabSpec1,SecondFragment.getClass(),null);

//            Fragment MeFragment = new BaoliaoFragment();
            Fragment MeFragment = new ServerFragment();
            TabHost.TabSpec tabSpec2 =
                    tabHost.newTabSpec(str[2]).setIndicator(view3);
            tabHost.addTab(tabSpec2,MeFragment.getClass(),null);


            Fragment MoreFragment = new ServerFragment();
            //Fragment MoreFragment = new WlcwBlank();
            TabHost.TabSpec tabSpec3 =
                    tabHost.newTabSpec(str[3]).setIndicator(view4);
            tabHost.addTab(tabSpec3,MoreFragment.getClass(),null);



            tabHost.getTabWidget().getChildTabViewAt(3).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(sharedPreferences.getString("access_token",null) != null){
                        Intent intent = new Intent(getBaseContext(), UserMessageActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                        startActivity(intent);
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
